Page({
  data: {
    favs: [],
    history: [],
    avatarUrl: wx.getStorageSync('avatarUrl') || '',
    username: wx.getStorageSync('username') || '',
    following: 0,
    followers: 0
  },
  onShow(){
    this.setData({
      favs: wx.getStorageSync('favs') || [],
      history: wx.getStorageSync('playHistory') || [],
      username: wx.getStorageSync('username') || ''
    })
    if(this.getTabBar && this.getTabBar()){
      this.getTabBar().updateActive()
    }
  },
  goFav(){
    wx.navigateTo({ url: '/pages/favs/favs' })
  },
  goHistory(){
    wx.navigateTo({ url: '/pages/history/history' })
  },
  goSettings(){
    wx.navigateTo({ url: '/pages/settings/settings' })
  },
  goAbout(){
    wx.navigateTo({ url: '/pages/about/about' })
  },
  uploadMusic(){
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      success: (res)=>{
        const file = res.tempFiles[0]
        if(file.name.endsWith('.mp3') || file.name.endsWith('.wav')){
          wx.showToast({ title: '音乐文件已选择', icon: 'success' })
          // 这里可以处理音乐文件，比如保存到本地或上传到服务器
        } else {
          wx.showToast({ title: '请选择音乐文件(.mp3/.wav)', icon: 'none' })
        }
      },
      fail: ()=> wx.showToast({ title: '选择失败', icon: 'none' })
    })
  },
  chooseAvatar(){
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      success: (res)=>{
        const path = res.tempFiles[0].tempFilePath
        this.setData({ avatarUrl: path })
        wx.setStorageSync('avatarUrl', path)
      }
    })
  },
  logout(){
    wx.removeStorageSync('token')
    wx.reLaunch({ url: '/pages/login/login' })
  }
})

